package edu.cmu.cs.cs214.hw9.client.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;

import edu.cmu.cs.cs214.hw9.client.controllers.Event;
import edu.cmu.cs.cs214.hw9.client.controllers.FacelookEvent;
import edu.cmu.cs.cs214.hw9.client.controllers.StatusSelector;
import edu.cmu.cs.cs214.hw9.datatypes.Status;
import edu.cmu.cs.cs214.hw9.datatypes.User;

/**
 * Displays a user's facelook profile. 
 */
public class ProfileScreen extends FacelookScreen {
	private static final long serialVersionUID = -4823058911503065876L;

	/**
	 * Actionlistener that inserts a status update from the current user into the
	 * database. 
	 */
	private class PostStatus implements ActionListener {
		private JTextField statusField;
		
		public PostStatus(JTextField statusField) {
			this.statusField = statusField;
		}
		
		public void actionPerformed(ActionEvent e) {
			fireEventOccured(new FacelookEvent(Event.POST_STATUS, statusField.getText()));
		}
	}
	
	/**
	 * Constructs a user profile screen. There are 2 users to be provided to this
	 * constructor; currentUser is the user who is currently logged in and looking
	 * at the profile screen, while profiledUser is the user whose profile is being
	 * displayed. The reason is to be able to differentiate between whether the current
	 * user is viewing his own profile or someone else's profile; if he is viewing 
	 * his own profile, then we need to allow him to post a new status; else, he needs
	 * to be able to add the profiled user as a friend.
	 * 
	 * @param currentUser The user who is browsing this profile screen.
	 * @param profiledUser The user whose profile should be displayed.
	 * @param statusList The list of status which have been posted by the profiled user.
	 * @param isFriend Whether the profiled user is a friend of the currently logged in user.
	 */
	public ProfileScreen(User currentUser, final User profiledUser, List<Status> statusList, boolean isFriend) {
		// Layout init.
		GridBagConstraints c = new GridBagConstraints();
		boolean sameUser = currentUser.equals(profiledUser);
		
		JLabel nameLabel = new JLabel(profiledUser.getFullname() + "\'s Profile");
		nameLabel.setForeground(Color.white);
		
		// Buttons for friend and status management.
		JButton newsButton = new JButton("Back to newsfeed");
		JButton addFriendButton = new JButton("Add friend");
		JButton removeFriendButton = new JButton("Remove as friend");
		JButton postStatusButton = new JButton("Post status");

		JTextField newStatusField = new JTextField();
		newStatusField.setColumns(20);
		
		JList statusPanelList = new JList();
		// Initialize the status list if they are friends.
		if (isFriend) statusPanelList = new JList(statusList.toArray(new Status[statusList.size()]));
		
		// We use a custom renderer for the status list since Status is our own datatype.
		statusPanelList.setCellRenderer(new StatusRenderer());
		statusPanelList.addListSelectionListener(new StatusSelector(this));
		statusPanelList.setBackground(new Color(59, 89, 152));

		// Define actions for the buttons.
		
		// Make the "back to newsfeed" button go back to the newsfeed.
		newsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireEventOccured(new FacelookEvent(Event.GO_TO_FEED, null));
			}	
		});
		
		// Add friend button adds the profiled user as a friend.
		addFriendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireEventOccured(new FacelookEvent(Event.ADD_FRIEND, profiledUser));
			}
		});
		
		// Remove friend button removes the profiled user as a friend.
		removeFriendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireEventOccured(new FacelookEvent(Event.REMOVE_FRIEND, profiledUser));
			}
		});
		
		// Make the post status button post a status.
		postStatusButton.addActionListener(new PostStatus(newStatusField));
		
		// Add components to the layout.
		
		// 1st Row
		c.gridx = 0;
		add(newsButton, c);
		
		c.gridx = 1;
		c.insets = new Insets(0, 10, 0, 10);
		add(nameLabel, c);

		c.gridx = 2;
		c.insets = new Insets(0, 0, 0, 0);
		if (!isFriend) add(addFriendButton, c);
		else if (!sameUser) add(removeFriendButton, c);

		if (currentUser.equals(profiledUser)) {
			// 2nd Row
			c.gridx = 0;
			c.gridwidth = (!sameUser) ? 2 : 1;
			add(newStatusField, c);
			
			c.gridx = 1;
			c.gridwidth = 1;
			add(postStatusButton, c);
		}
		
		// 3rd Row
		c.gridx = 0;
		c.gridwidth = 3;
		c.fill = GridBagConstraints.HORIZONTAL;
		add(new JScrollPane(statusPanelList), c);
	}
}
